package com.CuandoLlegaAndroid.Helpers;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.CuandoLlegaAndroid.DataHelper;
import com.CuandoLlegaAndroid.R;
import com.CuandoLlegaAndroid.Custom.CustomToast;
import com.CuandoLlegaAndroid.DDLModels.Street;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


public final class SpinnerHelper 
{
	public static void LoadSnniper(ArrayList<Street> streets, Spinner _SpinnerStreet, Context t, Object _lastSelection) 
	{
		try {

			ArrayAdapter<Street> _AdapterStreet = new ArrayAdapter<Street>(t, android.R.layout.simple_spinner_item);
			_AdapterStreet.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			_AdapterStreet.add(new Street(t.getString(R.string.seleccione), ""));

			for (int i = 0; i <= streets.size() - 1; i++) {
				Street o = streets.get(i);
				_AdapterStreet.add(o);

			}
			_SpinnerStreet.setAdapter(_AdapterStreet);

			SetLastSelection(_SpinnerStreet, _lastSelection);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}


	public static void LoadSnniper(String html, final String idLine, final String nameLine,final String idStreet, final String nameStreet, Spinner _SpinnerStreet, final Context t, Object _lastSelection) {
		String street = html;

		try {

			JSONObject jObject = new JSONObject("{\"Calles\":" + street + "}");
			JSONArray jArray = new JSONArray(jObject.getString("Calles"));
			final ArrayAdapter<Street> _AdapterStreet = new ArrayAdapter<Street>(t, android.R.layout.simple_spinner_item);
			_AdapterStreet.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			_AdapterStreet.add(new Street(t.getString(R.string.seleccione), ""));

			for (int i = 0; i <= jArray.length() - 1; i++) {
				JSONObject o = jArray.getJSONObject(i);

				String id = o.getString("id");
				String desc = o.getString("desc");
				_AdapterStreet.add(new Street(desc, id));

			}

			Thread checkUpdate = new Thread() {  
				public void run() {
					try{
						ArrayList<Street> list = new ArrayList<Street>();
						for (int i = 0; i <= _AdapterStreet.getCount() - 1; i++) {
							if(i!=0){
								list.add(_AdapterStreet.getItem(i));
							}
						}

						DataHelper dh = new DataHelper(t);
						if(idStreet==null)
						{
							dh.insertStreets(idLine, nameLine, list);
						}
						else
						{
							dh.insertIntersectStreets(idLine, nameLine, idStreet, nameStreet, list);
						}
					}
					catch(Exception e){

					}
				}
			};
			checkUpdate.start();

			_SpinnerStreet.setAdapter(_AdapterStreet);
			SetLastSelection(_SpinnerStreet, _lastSelection);

		} catch (JSONException e) {

			e.printStackTrace();
			CleanSnniper(_SpinnerStreet, t);

			CustomToast toast = new CustomToast(t);
			toast.ShowToast(t.getString(R.string.noSePudoListar));


		} catch (Exception e) {

			e.printStackTrace();
			CleanSnniper(_SpinnerStreet, t);

			CustomToast toast = new CustomToast(t);
			toast.ShowToast(t.getString(R.string.noSePudoListar));

		}

	}

	public static void CleanSnniper(Spinner _SpinnerStreet, Context t) 
	{
		ArrayAdapter<Street> _AdapterStreet = new ArrayAdapter<Street>(t, android.R.layout.simple_spinner_item);
		_AdapterStreet.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		_SpinnerStreet.setAdapter(_AdapterStreet);

	}

	private static void SetLastSelection(Spinner _Spinner, Object _lastSelection)
	{
		if(_lastSelection!=null) {

			Street lastStreet = (Street)_lastSelection;

			ArrayAdapter<Street> adapter = (ArrayAdapter<Street>)_Spinner.getAdapter();

			for(int a=0;a < adapter.getCount();a++)
			{
				Street s = adapter.getItem(a);
				if(s.ID.equals(lastStreet.ID)){
					_Spinner.setSelection(a);
					return;
				}
			}

		}
	}
}

